<?php
class ReportController extends AppController {
	public $uses = array('ThongKe','ChiTieuThongKe','Tthc','ChiTieuPhanLoai', 'PhanLoaiHoso');
	public $helpers = array('Xls','Csv');
	
	public function beforeFilter() {
		$this->Auth->allow('*');
		$this->layout = 'report';
		$this->set("actionName", $this->request->params['action']);
	}
	
	public function index() {
		$this->redirect(array('action' => 'baocaoTk'));
	}
	
	public function baocaoTk() {
		if (!empty($this->request->data)) {
			// debug($this->request->data);
			$fromDate = $this->convertStrDate($this->request->data['Baocao']['from']);
			$toDate = $this->convertStrDate($this->request->data['Baocao']['to']);
			$tthcId = $this->request->data['Baocao']['tthc_id'];
			
			$chiTieus = $this->ChiTieuThongKe->getBaoCaoTK($fromDate, $toDate, $tthcId);
			// debug($chiTieus);
			
			$this->set('selectedTthc',$tthcId);
			$this->set("chiTieus", $chiTieus);
			$this->Session->write("list", $chiTieus);
		}
		
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$this->set("tthcs", $tthcs);
	}
	
	public function baocaoPl() {
		if (!empty($this->request->data)) {
			// debug($this->request->data);
			
			$fromDate = $this->convertStrDate($this->request->data['Baocao']['from']);
			$toDate = $this->convertStrDate($this->request->data['Baocao']['to']);
			$tthcId = $this->request->data['Baocao']['tthc_id'];
			
			$chiTieus = $this->ChiTieuPhanLoai->getBaoCaoPL($fromDate, $toDate, $tthcId);
			$this->set("chiTieus", $chiTieus);
			
			// debug($chiTieus);	
		}
		
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$this->set("tthcs", $tthcs);
	}
	
	public function baocaoThp() {
		$this->render('../systems/under_contruction');
	}
	public function export() {
		//debug("export");
		$this->layout = '';
		$chiTieus = $this->Session->read("list");
		$csvData[] = array('STT','Tên chỉ tiêu','Mã quốc gia','Đơn vị tính','Đầu kỳ','Trong kỳ','Cuối kỳ');
		foreach ($chiTieus as $key => $chiTieu) {
			$csvData[] = array(
				$key + 1,
				$chiTieu['ct']['ten'],
				$chiTieu['ct']['ma'],
				$chiTieu['ct']['don_vi_tinh'],
				$chiTieu['dauky']['dauky'],
				$chiTieu['trongky']['trongky'],
				$chiTieu['cuoiky']['cuoiky']
			);
		}
		$this->layout = '';
		$this->set('lines', $csvData);
		$this->set('fileName', 'baocaothongke_'.date('Y_m_d')."csv");
	}
	
}
	